package com.zjitc.exception;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public ModelAndView modelAndView(HttpServletRequest request, BusinessException bE){
        ModelAndView mav = new ModelAndView("error");
        mav.addObject("exception", bE.getMessage());
        mav.addObject("url",request.getRequestURL());
        return mav;
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception exception){
        ModelAndView mav = new ModelAndView("error");
        mav.addObject("exception", exception.getMessage());
        mav.addObject("url", request.getRequestURL());
        return mav;
    }
}
